//
//  HonorTableViewCell.swift
//  BellManage
//
//  Created by Bestsch on 16/3/31.
//  Copyright © 2016年 wsl. All rights reserved.
//

import UIKit

class HonorTableViewCell: UITableViewCell,UICollectionViewDataSource,UICollectionViewDelegate {
    
    var bgView, photoBgView: UIView!
    var titleLabel,timeLabel: UILabel!
    var oneImage: PicProgressView!
    var photoCollectionView: UICollectionView!
    
    var dic: JSON!
    
    let collectionIdentifier = "PicCollectionViewCellIdentifier"
    let imageArray = NSMutableArray()
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    init(dictionary: JSON, reuseIdentifier: String?) {
        self.dic = dictionary
        super.init(style: UITableViewCellStyle.default, reuseIdentifier: reuseIdentifier)
        setUp()
    }
    
    /**
     初始化设置各种控件
     */
    func setUp() {
        
        bgView = UIView.init(frame: CGRect.zero)
        bgView.backgroundColor = UIColor.init(hexString: "f6f6f6", alpha: 1)
        
        titleLabel = UILabel.init(frame: CGRect.zero)
        titleLabel.numberOfLines = 0
        titleLabel.font = UIFont.systemFont(ofSize: 14)
        titleLabel.textColor = UIColor.black
        titleLabel.textAlignment = NSTextAlignment.left
        
        timeLabel = UILabel.init(frame: CGRect.zero)
        timeLabel.font = UIFont.boldSystemFont(ofSize: 14)
        timeLabel.textColor = UIColor.darkText
        timeLabel.textAlignment = NSTextAlignment.left
        
        
        photoCollectionView = UICollectionView.init(frame: CGRect.zero, collectionViewLayout: photoFlowLayout.init())
        photoCollectionView.backgroundColor = UIColor.white
        photoCollectionView.register(PicCollectionViewCell.self, forCellWithReuseIdentifier: collectionIdentifier)
        photoCollectionView.scrollsToTop = true
        photoCollectionView.delegate = self
        photoCollectionView.dataSource = self
        
        photoBgView = UIView.init(frame: CGRect.zero)
        photoBgView.backgroundColor = UIColor.white
        
        
        
        oneImage = PicProgressView.init(frame: CGRect.zero)
        oneImage.isUserInteractionEnabled = true
        let showImage = UITapGestureRecognizer.init(target: self, action: #selector(HonorTableViewCell.showImage))
        oneImage.addGestureRecognizer(showImage)
        
        
        
        var title = dic["Honor"].stringValue
        title = title.removingPercentEncoding!
        let time = dic["edittime"].stringValue
        let imageUrl = dic["HonorUrl"].stringValue
        
        titleLabel.text = title
        timeLabel.text = getTimeSinceNow(time).replacingOccurrences(of: ".", with: "")
        
        self.contentView.addSubview(bgView)
        self.contentView.addSubview(photoBgView)
        self.contentView.addSubview(titleLabel)
        self.contentView.addSubview(timeLabel)
        
        
        var height: CGFloat = 0.0
        if imageUrl.characters.count == 0 {
            photoBgView.removeFromSuperview()
            height = 0.0
        }else {
            
            let array = imageUrl.components(separatedBy: "|") as NSArray
            
            if array.count == 1 {
                self.contentView.addSubview(oneImage)
                
                let photo = imageUrl.replacingOccurrences(of: "../..", with: "")
                let imageurl = server + priImg + photo + "&width=400&mode=W"
                oneImage.imageUrl = imageurl
            }else {
                self.contentView.addSubview(photoCollectionView)
                imageArray.addObjects(from: array as [AnyObject])
            }
            height = getPhotoCollectionHeight(array)
        }
        /// 头像，间隔，上下间隔，时间，图片等，内容
        let cellHeight: CGFloat = 10.0 + 20.0 + 30.0 + height + getLabelHeight(title, font: UIFont.systemFont(ofSize: 14) , wigth: screenW - 40)
        
        bgView.frame = CGRect(x: 10, y: 10, width: screenW - 20, height: cellHeight - 20)
        bgView.layer.cornerRadius = 4;
        bgView.layer.shadowOffset = CGSize(width: 0, height: 1);  // 设置阴影的偏移量
        bgView.layer.shadowRadius = 2.0;  // 设置阴影的半径
        bgView.layer.shadowColor = UIColor.lightGray.cgColor; // 设置阴影的颜色为黑色
        bgView.layer.shadowOpacity = 0.8; // 设置阴影的不透明度
        
        titleLabel.frame = CGRect(x: bgView.frame.minX+10, y: bgView.frame.minY+5, width: screenW - 40, height: getLabelHeight(title, font: UIFont.systemFont(ofSize: 14) , wigth: screenW - 40));
        
        photoBgView.frame = CGRect(x: bgView.frame.minX, y: titleLabel.frame.maxY+5, width: bgView.frame.width, height: height)
        oneImage.frame = CGRect(x: photoBgView.frame.minX+5, y: photoBgView.frame.minY+5, width: photoBgView.frame.width-10, height: photoBgView.frame.height-10);
        photoCollectionView.frame = CGRect(x: photoBgView.frame.minX+5, y: photoBgView.frame.minY+5, width: photoBgView.frame.width-10, height: self.photoBgView.frame.height-10);
        
        timeLabel.frame = CGRect(x: bgView.frame.minX + 10, y: photoBgView.frame.maxY, width: screenW/2, height: 30)
        //类型
        
        
    }
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return imageArray.count
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell: PicCollectionViewCell = collectionView.dequeueReusableCell(withReuseIdentifier: collectionIdentifier, for: indexPath) as! PicCollectionViewCell
        cell.backgroundColor = UIColor.white
        var photo = imageArray.object(at: (indexPath as NSIndexPath).row) as! String
        photo = photo.replacingOccurrences(of: "../..", with: "")
        let imageurl = server + priImg + photo + "&width=300&mode=W"
        cell.imageUrl = imageurl
        return cell;
        
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        
        let imageUrl = dic["HonorUrl"].stringValue
        let index = String((indexPath as NSIndexPath).row)
        NotificationCenter.default.post(name: Notification.Name(rawValue: "showBigImage"), object: nil, userInfo: ["url" : imageUrl, "index" : index])
        
    }
    /**
     显示大图
     */
    func showImage() {
        let imageUrl = dic["HonorUrl"].stringValue
        let index = "0"
        NotificationCenter.default.post(name: Notification.Name(rawValue: "showBigImage"), object: nil, userInfo: ["url" : imageUrl, "index" : index])
    }
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }
    
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        
        // Configure the view for the selected state
    }
    
}
